#!/usr/bin/env python2.5

## Provides the commands for use in the Tutor, when the user/player is not making a play.

## Usage:  ~       (meant to be used modularly)

import answer
from answer import write

def help():
	"""F1"""
	write("""
	To make a valid play, simply type a statement in a form resembling the "If..., then..." proposition.
	A valid play will behave grammatically, and use words that the Tutor knows.  Be prepared to confirm that we have the same definition of nouns that you have.
	If you wish to use a word that I don't know, you will be asked to define it.
	If you want to skip that step, and define a word on your own, just put its definition in parantheses after it.
	And, if you would prefer to leave it undefined, don't put anything in the parantheses but elipsis (...).
	Finally, if a sentence can be read more than one way, I'll tell you and ask you to rephrase it.
	All in all, I want you to be able to make plays which will contribute positively to the question being begged.  As long as everything is in order, I'm sure you'll be able to make some very valid points and have your theses stand the test of time.
	""")

def version():
	"""Version info.  If you wish to update these, I ask that you leave the old info behind."""
	## Version 1.0.0:  September 10, 2009; Josh "Lain" Johnson
	## Version 1.0.1:  September 21, 2009; Josh "Lain" Johnson
	write('NLP Tutor, version 1.0.1.  Created by Josh "Lain" Johnson on 21 September 2009.')

def about():
	"""Tells a little bit about the Tutor."""
	version()
	write("The NLP Tutor helps players to make valid plays, in order to interface effectively with Revelator.")

def commands():
	"""List of commands."""
	write("""LIST OF COMMANDS:\n
	"help"       -- see how to make valid plays in Revelator using this Tutor
	"version"    -- get version info about this Tutor
	"about"      -- learn what the Tutor is
	"sample"     -- see what a sample sentence would look like
	"quit"       -- leave me by the wayside
	"commands"   -- what got you here
	""")

def sample():
	"""Show sample plays (which will not be passed on to Revelator)."""
	write("""
	SAMPLE PLAYS
	------------

	Normal play:

		If the person is over the age of 62, then s/he may retire from the workforce.

	You may be asked to define words which I don't know.  If you want to define a word without being asked, just flank its definition with parantheses, as follows:

		If the Vikings defeat the Packers in the NFC Championship, then it will upset a lot of cheeseheads (Packers fans).
	
	You could also leave a word undefined, for whatever reasons, by putting ellipses in the parantheses.

		If this widget (...) performs the task it should, then our 3rd-quarter revenue will increase threefold.

	Of course, you are not bound to use "if...then" statements with me.  As long as I can rephrase it to an "if...then" statement, then I am satisfied.

		Since Seras Victoria is bitten by Alacard in the first episode, therefore she becomes his servant for the remainder of the series.

	I hope this helps you to make satisfactory plays.  Remember that, the better your plays conform to my few rules, the faster you will be able to contribute, and the likelier your plays are to persist as more plays are made.
	
	""")

def quit(string):
	"""Quits the Tutor."""
	return (string.lower() in ["quit", "q", "exit", "stop", "bail", "x"])

def error(command):
	"""Prints an error message relative to the command set."""
	write("""I'm sorry, but "%s" is not a valid command.  Please type one of my standard commands, make a play, or type "quit" to exit.\n""" % command)
	commands()

def aCommand(command):
	"""Determines whether something is a command.  If so, execute it.  If it is neither that nor a play, throw an error."""
	if (len(command.split()) > 1):	return 0
	else:
		if (not quit(command)):
			command = convert(command)
			write("")
			try:
				exec("%s()" % command)
			except(Exception):
				error(command)
			return 1
		else:	return -1

def convert(command):
	"""Converts a synonymous command with one of the standard command names for the Tutor."""
	if (command.lower() in ["?", "help", "h", "question", "f1"]):	command = "help"
	if (command.lower() in ["about", "a"]):	command = "about"
	if (command.lower() in ["ver", "version", "v", "release"]):	command = "version"
	if (command.lower() in ["sample", "s", "example", "ensample"]):	command = "sample"
	return (command)

def test1():
	import sys
	commands()
	help()
	version()
	about()
	sample()
	test = "version"
	exec("%s()" % test)
	try:
		test = "ver"
		exec("%s()" % test)
	except(Exception):
		error(test)

def test2():
	comm = ""
	while (not quit(comm)):
		if (not aCommand(comm)):	print "%s was not a command." % comm
		comm = raw_input("Your next command:  ")

if (__name__ == "__main__"):
	# test1()
	test2()
